package com.example.pinkcat.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerTabStrip;
import androidx.viewpager.widget.ViewPager;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.pinkcat.R;
import com.example.pinkcat.activity.adapter.SP_recyleview_adapter;
import com.example.pinkcat.activity.adapter.SP_viewpager_adapter;
import com.example.pinkcat.activity.api.getData.GetData;


import java.util.ArrayList;
import java.util.Objects;

public class SeachPage extends AppCompatActivity {
    Context context=this;
    ViewPager viewPager;//viewpager组件
    PagerTabStrip pagerTab;  //PagerTabStrip组件
    ArrayList<View> views=new ArrayList<>();

    EditText search;


    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        if (newConfig.fontScale != 1)//非默认值
            getResources();
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        if (res.getConfiguration().fontScale != 1) {//非默认值
            Configuration newConfig = new Configuration();
            newConfig.setToDefaults();//设置默认
            res.updateConfiguration(newConfig, res.getDisplayMetrics());
        }
        return res;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Objects.requireNonNull(getSupportActionBar()).hide();//去掉标题栏
        //        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉信息栏
        setContentView(R.layout.activity_seach_page);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }//深色信息栏

        //返回监听
        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        search=findViewById(R.id.searchedit);
        viewPager=findViewById(R.id.ViewPager_search);
        pagerTab=findViewById(R.id.pagerTableStrip_search_tile);

        //初始化viewpager
        SetViews();
        ArrayList<SP_recyleview_adapter> adapters=new ArrayList<>();
        for (int i=0;i<6;i++){
            SP_recyleview_adapter adapter=new SP_recyleview_adapter(context,10,i);
            adapters.add(adapter);
        }
        SP_viewpager_adapter video_viewpager_adapter=new SP_viewpager_adapter(new String[]{"PinkCat视频","综艺","动漫",  "电视剧","电影","用户",},views,context,adapters);
        viewPager.setAdapter(video_viewpager_adapter);
        pagerTab.setTabIndicatorColor(getResources().getColor(R.color.pink));//标题栏指示器的颜色
        //pagerTab.setTextColor(getResources().getColor(R.color.pink));  //设置标题字体颜色
        //pagerTab.setTextSpacing(20);
        pagerTab.setTextSize(1, 16);  //设置标题字体大小
        pagerTab.setDrawFullUnderline(false);//下横线
        viewPager.setCurrentItem(0);//设定默认选中的
        findViewById(R.id.searchico).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (search.getText().toString().length()<1){
                    Toast.makeText(context,"搜索字符过短",Toast.LENGTH_SHORT).show();
                    return;
                }else {
                    GetData.search(context,search.getText().toString(),adapters);
                }

            }
        });
        search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    if (search.getText().toString().length()<1){
                        Toast.makeText(context,"搜索字符过短",Toast.LENGTH_SHORT).show();
                        return true;
                    }else {
                        GetData.search(context,search.getText().toString(),adapters);
                    }
                }
                return false;
            }
        });
    }

    public void SetViews(){
        views.add(getLayoutInflater().inflate(R.layout.recycle_search_page, null));
        views.add(getLayoutInflater().inflate(R.layout.recycle_search_page, null));
        views.add(getLayoutInflater().inflate(R.layout.recycle_search_page, null));

        views.add(getLayoutInflater().inflate(R.layout.recycle_search_page, null));
        views.add(getLayoutInflater().inflate(R.layout.recycle_search_page, null));
        views.add(getLayoutInflater().inflate(R.layout.recycle_search_page, null));
    }
}